namespace SimpleWebServices.Configuration
{
    using System.Xml.XPath;
    using SimpleWebServices;
    using Xunit;

    public sealed class HttpSettingsFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<HttpSettings>()
                .DerivesFrom<object>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("http")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            HttpSettings actual = (
                "<http console.write='true'>" +
                    "<headers>" +
                        "<absent>Server</absent>" +
                        "<absent>X-Powered-By</absent>" +
                    "</headers>" +
                "</http>").Deserialize<HttpSettings>();

            Assert.NotNull(actual);

            Assert.True(actual.ConsoleWrite);
            Assert.NotEmpty(actual.Headers.Absent);
        }

        [Fact]
        public void deserializeEmpty()
        {
            HttpSettings actual = "<http />".Deserialize<HttpSettings>();

            Assert.NotNull(actual);

            Assert.False(actual.ConsoleWrite);
            Assert.Empty(actual.Headers.Absent);
        }

        [Fact]
        public void serialize()
        {
            HttpSettings obj = new HttpSettings
            {
                ConsoleWrite = true,
                Headers =
                {
                    Absent =
                    {
                        "Server",
                        "X-Powered-By"
                    }
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("2=count(/http[@console.write='true']/headers/absent[text()])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new HttpSettings());
        }

        [Fact]
        public void prop_ConsoleWrite()
        {
            Assert.True(new PropertyExpectations(typeof(HttpSettings).GetProperty("ConsoleWrite"))
                .XmlAttribute("console.write")
                .IsAutoProperty<bool>()
                .Result);
        }

        [Fact]
        public void prop_Headers()
        {
            Assert.True(new PropertyExpectations(typeof(HttpSettings).GetProperty("Headers"))
                .XmlElement("headers")
                .TypeIs<HttpHeaderSettings>()
                .DefaultValueIsNotNull()
                .Result);
        }
    }
}